package com.appspot.diasporajava.dao.hibernate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;

import com.appspot.diasporajava.dao.GenericDao;

public abstract class GenericDaoHibernateImpl<T, ID extends Serializable>
		implements GenericDao<T, ID> {

	private Class<T> persistentClass;
	private Session session;
	
	@Autowired
	private SessionFactory sessionFactory;

	public GenericDaoHibernateImpl() {
		this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
	
	}

	@SuppressWarnings("unchecked")
	public void setSession(Session s) {
		this.session = s;
	}

	protected Session getSession() {
		if (session == null){
			session=sessionFactory.openSession();
		}
		return session;
	}

	public Class<T> getPersistentClass() {
		return persistentClass;
	}

	@SuppressWarnings("unchecked")
	public T findById(ID id, boolean lock) {
		T entity;
		if (lock)
			entity = (T) getSession().load(getPersistentClass(), id,
					LockOptions.UPGRADE);
		else
			entity = (T) getSession().load(getPersistentClass(), id);

		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		Example example = Example.create(exampleInstance);
		for (String exclude : excludeProperty) {
			example.excludeProperty(exclude);
		}
		crit.add(example);
		return crit.list();
	}

	@SuppressWarnings("unchecked")
	public T makePersistent(T entity) {
		getSession().saveOrUpdate(entity);
		return entity;
	}

	public void makeTransient(T entity) {
		getSession().delete(entity);
	}

	public void flush() {
		getSession().flush();
	}

	public void clear() {
		getSession().clear();
	}

	/**
	 * Use this inside subclasses as a convenience method.
	 */
	@SuppressWarnings("unchecked")
	protected List<T> findByCriteria(Criterion... criterion) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		for (Criterion c : criterion) {
			crit.add(c);
		}
		return crit.list();
	}

	@Override
	public long countAll(Map params) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public T create(T t) {
		getSession().saveOrUpdate(t);
		return t;
	}

	public void delete(T t) {
		getSession().delete(t);

	}

	@Override
	public void delete(ID id) {
		T t = find(id);
		delete(t);
	}

	@Override
	public T find(ID id) {
		return (T) getSession().load(getPersistentClass(), id);
	}

	@Override
	public T update(T t) {
		getSession().saveOrUpdate(t);
		return t;
	}

	@Override
	public List<T> list() {
		return getSession().createCriteria(persistentClass).list();
		//return findByCriteria();
	}

}
